package com.se.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.pulp.service.OrgnizationInfoService;
import com.se.common.StringUtils;
import com.se.model.CommonSessionUser;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LoginAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private String REQUEST_RESULT = "ok";
	static Logger logger = Logger.getLogger(LoginAction.class);
	
	public LoginAction() {
	}
	
	public String excute() throws Exception {
	
		
		try{

			HttpServletRequest request	= (HttpServletRequest)ServletActionContext.getRequest();
			HttpSession session = request.getSession();
        	CommonSessionUser user = (CommonSessionUser)((HttpServletRequest)request).getSession().getAttribute(CommonSessionUser.ATTRIBUTE_NAME);
        	
        	user = null;
        	((HttpServletRequest)request).getSession().setAttribute(CommonSessionUser.ATTRIBUTE_NAME, user);
			
		}  catch (Exception ex) {
        	REQUEST_RESULT = "FAILED";
            ex.printStackTrace();
        }
		return REQUEST_RESULT;
	}
	
	public String loginUser() throws Exception {
		
		HttpServletRequest request	= (HttpServletRequest)ServletActionContext.getRequest();
		
		
		 ServletContext servletContext = ServletActionContext.getServletContext();
			WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			OrgnizationInfoService orgnizationInfoService = (OrgnizationInfoService) wac.getBean("orgnizationInfoService");
			
			String empno = request.getParameter("user_id");
			String pwd = request.getParameter("user_pw");
			 
			
//			List loginList = orgnizationInfoService.getLoginUserMapping(empno, pwd);
		
		 
		System.out.println("user_id===  " + empno);
		System.out.println("user_pw===  " + pwd);
//		
//		System.out.println("loginList.size()===     " + loginList.size());
		
		try {
			
			
//			if (loginList.size() == 1 ) {
				
				
//				Map resultMap = (Map)loginList.get(0);
//	        	Map cell = new HashMap();
//		        	
		        	String USERID = "";
		        	String USERNAME = "";
		        	String NICKNAME = "";
//		        	String MSTRID = "admin";
//		        	String MSTRPW = "admin";
//		        	String MSTRID = (String) resultMap.get("MSTRID");
//		        	String MSTRPW = (String) resultMap.get("MSTRID");
//		        	String DEPTCODE = "";
		        	CommonSessionUser user = null;
		        	
		        	user = new CommonSessionUser();
	            	user.setId(USERID);
	            	user.setKey(USERID);
	            	user.setName(USERNAME);
	            	user.setNickname(NICKNAME);
//	            	user.setMstrId(MSTRID);
//	            	user.setOlapKey(MSTRPW);
	            	user.setAuthenticatedUser(true);
//	            	user.setDeptId(DEPTCODE);
	                	user.setBbsBoardManager(true);
	                	user.setAdministrator(true);
	            	((HttpServletRequest)request).getSession().setAttribute(CommonSessionUser.ATTRIBUTE_NAME, user);
			
//			}else{
//				request.setAttribute("ERRCODE", "1");
//				REQUEST_RESULT = "fail";
//				
//			}
	            	
		} catch(Exception e){
			e.printStackTrace();
		} 
		
		return REQUEST_RESULT;
    }
	
	
}